package model

import (
	"fmt"
	"github.com/jinzhu/gorm"
)

type _UserMgr struct {
	*_BaseMgr
}

// UserMgr open func
func UserMgr(db *gorm.DB) *_UserMgr {
	if db == nil {
		panic(fmt.Errorf("UserMgr need init by db"))
	}
	return &_UserMgr{_BaseMgr: &_BaseMgr{DB: db, isRelated: gloabIsRelated}}
}

// GetTableName get sql table name.获取数据库名字
func (obj *_UserMgr) GetTableName() string {
	return "user"
}

// Get 获取
func (obj *_UserMgr) Get() (result User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Find(&result).Error

	return
}

// Gets 获取批量结果
func (obj *_UserMgr) Gets() (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Find(&results).Error

	return
}

//////////////////////////option case ////////////////////////////////////////////

// WithID id获取
func (obj *_UserMgr) WithID(ID int) Option {
	return optionFunc(func(o *options) { o.query["id"] = ID })
}

// WithName name获取
func (obj *_UserMgr) WithName(Name string) Option {
	return optionFunc(func(o *options) { o.query["name"] = Name })
}

// WithSex sex获取
func (obj *_UserMgr) WithSex(Sex string) Option {
	return optionFunc(func(o *options) { o.query["sex"] = Sex })
}

// WithAge age获取
func (obj *_UserMgr) WithAge(Age int) Option {
	return optionFunc(func(o *options) { o.query["age"] = Age })
}

// WithUpdatedAt updated_at获取
func (obj *_UserMgr) WithUpdatedAt(UpdatedAt int) Option {
	return optionFunc(func(o *options) { o.query["updated_at"] = UpdatedAt })
}

// WithCreatedAt created_at获取
func (obj *_UserMgr) WithCreatedAt(CreatedAt int) Option {
	return optionFunc(func(o *options) { o.query["created_at"] = CreatedAt })
}

// WithPhone phone获取
func (obj *_UserMgr) WithPhone(Phone string) Option {
	return optionFunc(func(o *options) { o.query["phone"] = Phone })
}

// GetByOption 功能选项模式获取
func (obj *_UserMgr) GetByOption(opts ...Option) (result User, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&result).Error

	return
}

// GetByOptions 批量功能选项模式获取
func (obj *_UserMgr) GetByOptions(opts ...Option) (results []*User, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.Table(obj.GetTableName()).Where(options.query).Find(&results).Error

	return
}

//////////////////////////enume case ////////////////////////////////////////////

// GetFromID 通过id获取内容
func (obj *_UserMgr) GetFromID(ID int) (result User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("id = ?", ID).Find(&result).Error

	return
}

// GetBatchFromID 批量唯一主键查找
func (obj *_UserMgr) GetBatchFromID(IDs []int) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("id IN (?)", IDs).Find(&results).Error

	return
}

// GetFromName 通过name获取内容
func (obj *_UserMgr) GetFromName(Name string) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("name = ?", Name).Find(&results).Error

	return
}

// GetBatchFromName 批量唯一主键查找
func (obj *_UserMgr) GetBatchFromName(Names []string) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("name IN (?)", Names).Find(&results).Error

	return
}

// GetFromSex 通过sex获取内容
func (obj *_UserMgr) GetFromSex(Sex string) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("sex = ?", Sex).Find(&results).Error

	return
}

// GetBatchFromSex 批量唯一主键查找
func (obj *_UserMgr) GetBatchFromSex(Sexs []string) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("sex IN (?)", Sexs).Find(&results).Error

	return
}

// GetFromAge 通过age获取内容
func (obj *_UserMgr) GetFromAge(Age int) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("age = ?", Age).Find(&results).Error

	return
}

// GetBatchFromAge 批量唯一主键查找
func (obj *_UserMgr) GetBatchFromAge(Ages []int) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("age IN (?)", Ages).Find(&results).Error

	return
}

// GetFromUpdatedAt 通过updated_at获取内容
func (obj *_UserMgr) GetFromUpdatedAt(UpdatedAt int) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("updated_at = ?", UpdatedAt).Find(&results).Error

	return
}

// GetBatchFromUpdatedAt 批量唯一主键查找
func (obj *_UserMgr) GetBatchFromUpdatedAt(UpdatedAts []int) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("updated_at IN (?)", UpdatedAts).Find(&results).Error

	return
}

// GetFromCreatedAt 通过created_at获取内容
func (obj *_UserMgr) GetFromCreatedAt(CreatedAt int) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("created_at = ?", CreatedAt).Find(&results).Error

	return
}

// GetBatchFromCreatedAt 批量唯一主键查找
func (obj *_UserMgr) GetBatchFromCreatedAt(CreatedAts []int) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("created_at IN (?)", CreatedAts).Find(&results).Error

	return
}

// GetFromPhone 通过phone获取内容
func (obj *_UserMgr) GetFromPhone(Phone string) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("phone = ?", Phone).Find(&results).Error

	return
}

// GetBatchFromPhone 批量唯一主键查找
func (obj *_UserMgr) GetBatchFromPhone(Phones []string) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("phone IN (?)", Phones).Find(&results).Error

	return
}

//////////////////////////primary index case ////////////////////////////////////////////

// FetchByPrimaryKey primay or index 获取唯一内容
func (obj *_UserMgr) FetchByPrimaryKey(ID int) (result User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("id = ?", ID).Find(&result).Error

	return
}

// FetchByNameSexAgeIndexIndex  获取多个内容
func (obj *_UserMgr) FetchByNameSexAgeIndexIndex(Name string, Sex string, Age int) (results []*User, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("name = ? AND sex = ? AND age = ?", Name, Sex, Age).Find(&results).Error

	return
}
